
package utils;

import hasthi.common.HasthiException;
import hasthi.common.InvaliedResourceDataException;
import hasthi.metamodel.ResourceModel;
import hasthi.metamodel.resource.AbstractManagedResource;
import hasthi.metamodel.resource.ManagedResource;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.xml.namespace.QName;

public class DummyResourceModel implements ResourceModel{
    private Map<QName,ManagedResource> map = new HashMap<QName, ManagedResource>();
    
    public DummyResourceModel(int count) throws HasthiException, InvaliedResourceDataException{
        Random r = new Random();
        
        for(int i = 0;i<count;i++){
            ManagedResource resource = new DymmyManagedResource(r);
            map.put(resource.getQName(), resource);
        }
    }
    
    public ManagedResource[] getAllResources() throws HasthiException {
        return map.values().toArray(new ManagedResource[0]);
    }

    public ManagedResource getResource(QName name) throws HasthiException {
        return map.get(name);
    }

    public Collection<QName> getResources() {
        return map.keySet();
   }
    
    public class DymmyManagedResource extends AbstractManagedResource{
        public DymmyManagedResource(Random r) throws HasthiException, InvaliedResourceDataException{
            super(new QName("Resource"+r.nextInt()),null,"http://someUrl",chooseResourceType(r),"TypeA");
        }
        @Override
        public String getProperty(String name) throws HasthiException {
            return properties.get(name);
        }
    }
    
    public static String chooseResourceType(Random r){
        int val = Math.abs(r.nextInt()%4);
        switch(val){
            case 0:
                return ManagedResource.ResourceType.TransientService.toString();
            case 1:
                return ManagedResource.ResourceType.Service.toString();
            case 2:
                return ManagedResource.ResourceType.ComputeNode.toString();
            default:
                return ManagedResource.ResourceType.Host.toString();
                
        }
    }

    public void reload() throws HasthiException {
        // TODO Auto-generated method stub
        
    }
    
    
}

